import * as echarts from '../ec-canvas/echarts'; // 注意：为了减小包的体积，echarts.js只打包了雷达图
import {debounce} from '../../utils/util';
import {queryPlanGroupByYearApi} from '../../api/app';

const radarOption = {
  // 雷达图配置
  backgroundColor: '#ffffff',
  xAxis: {
    show: false,
  },
  radar: {
    // shape: 'circle',
    indicator: [
      {
        name: '工作',
        max: 10,
        color: '#3E3E3E',
      },
      {
        name: '学习',
        max: 10,
        color: '#3E3E3E',
      },
      {
        name: '娱乐',
        max: 10,
        color: '#3E3E3E',
      },
      {
        name: '生活',
        max: 10,
        color: '#3E3E3E',
      },
      {
        name: '其他',
        max: 10,
        color: '#3E3E3E',
      },
    ],
  },
  series: [
    {
      name: '能力点',
      type: 'radar',
      itemStyle: {
        color: '#ffa400',
      },
      areaStyle: {
        color: '#fff1df',
      },
      lineStyle: {
        color: '#febf52',
      },
      data: [
        {
          value: [0, 0, 0, 0, 0],
          name: '能力点',
        },
      ],
    },
  ],
};

let chart = null; // 图表实例
const initChart = function (canvas, width, height, dpr) {
  chart = echarts.init (canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr, // new
  });

  canvas.setChart (chart);
  chart.setOption (radarOption);
  return chart;
};

Page ({
  /**
   * 页面的初始数据
   */
  data: {
    name: wx.getStorageSync('nickName'),
    date: String (new Date ().getFullYear ()),
    currentYear: String (new Date ().getFullYear ()),
    StatusBar: 40,
    CustomBar: 84,
    opacity: 0,
    ec: {
      onInit: initChart,
    },
    info: {}, // 测评报告详情
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getEvaReportInfo()
  },
  onPageScroll: debounce (function (args) {
    const scrollTop = args[0].scrollTop;
    this.setData ({
      opacity: scrollTop / 800,
    });
  }, 10),
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '快来加入协同小程序吧~',
    };
  },
  bindDateChange: function (e) {
    console.log ('picker发送选择改变，携带值为', e.detail.value);
    this.setData ({
      date: e.detail.value,
    });
    this.getEvaReportInfo()
  },
  // 获取报告详情
  async getEvaReportInfo () {
    try {
      const res = await queryPlanGroupByYearApi ({
        planStartTime: this.data.date,
        openid: wx.getStorageSync ('openId'),
      });
      if (res.code === 200) {
        this.setData({
          info: res.result
        })
        radarOption.series[0].data[0].value = res.result.typeList.map(item=> item.number)
        if (chart) chart.setOption(radarOption)
      } else {
        wx.showToast ({
          title: res.msg,
          icon: 'none',
        });
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 返回事件
  back () {
    wx.navigateBack ({
      delta: 1,
      fail: () => {
        wx.switchTab ({
          url: '/pages/index/index',
        });
      },
    });
  },
});
